<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="小区名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="小区名称"></el-input>
    </el-form-item>
    <el-form-item label="省" prop="province">
      <el-input v-model="dataForm.province" placeholder="省"></el-input>
    </el-form-item>
    <el-form-item label="市" prop="city">
      <el-input v-model="dataForm.city" placeholder="市"></el-input>
    </el-form-item>
    <el-form-item label="县" prop="county">
      <el-input v-model="dataForm.county" placeholder="县"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="createtime">
      <el-input v-model="dataForm.createtime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="排序（从高到低）" prop="level">
      <el-input v-model="dataForm.level" placeholder="排序（从高到低）"></el-input>
    </el-form-item>
    <el-form-item label="状态（1正常，2禁用）" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态（1正常，2禁用）"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          province: '',
          city: '',
          county: '',
          createtime: '',
          level: '',
          status: ''
        },
        dataRule: {
          name: [
            { required: true, message: '小区名称不能为空', trigger: 'blur' }
          ],
          province: [
            { required: true, message: '省不能为空', trigger: 'blur' }
          ],
          city: [
            { required: true, message: '市不能为空', trigger: 'blur' }
          ],
          county: [
            { required: true, message: '县不能为空', trigger: 'blur' }
          ],
          createtime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          level: [
            { required: true, message: '排序（从高到低）不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态（1正常，2禁用）不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/backstage/community/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.community.name
                this.dataForm.province = data.community.province
                this.dataForm.city = data.community.city
                this.dataForm.county = data.community.county
                this.dataForm.createtime = data.community.createtime
                this.dataForm.level = data.community.level
                this.dataForm.status = data.community.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/backstage/community/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'province': this.dataForm.province,
                'city': this.dataForm.city,
                'county': this.dataForm.county,
                'createtime': this.dataForm.createtime,
                'level': this.dataForm.level,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
